package io;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

import org.junit.Test;

public class CopyFileDemo {
	@Test
	public void copy1() {
		try (FileInputStream fis = new FileInputStream("a.txt");
			 FileOutputStream fos = new FileOutputStream("copy.txt");) {
			
			int ch;
			while ( (ch = fis.read()) != -1) {
				fos.write(ch);
			}
			
			fos.flush();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@Test
	public void copy2() {
		try (FileInputStream fis = new FileInputStream("a.txt");
			 FileOutputStream fos = new FileOutputStream("copy2.txt");) {
			
			// fis.available() 计算文件的字节数， 开辟字节数组用来保存读取的内容
			byte[] content = new byte[fis.available()];
			fis.read(content);
			
			fos.write(content);
			fos.flush();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@Test
	public void copy3() throws FileNotFoundException, IOException {
		Files.copy(Paths.get("a.txt"),new FileOutputStream("copy3.txt"));
	}
}
